package org.openkanban.client.ui.footer;

import org.openkanban.client.login.LogIn;
import org.openkanban.client.ui.presenter.NewAvailableContactRequestCallback;
import org.openkanban.client.ui.presenter.PreparedRequest;

import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.types.FormLayoutType;
import com.smartgwt.client.types.Positioning;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.PickerIcon;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.FormItemClickHandler;
import com.smartgwt.client.widgets.form.fields.events.FormItemIconClickEvent;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VStack;

/**
 * GUI Element of the UserContactsStack.
 */
public class UserContactsStack extends VStack {

    public UserContactsStack(int height, int titleHeight) {
	setWidth("*");
	setHeight(height);

	Label userContactsLabel = new Label("User Contacts");
	userContactsLabel.setHeight(titleHeight);
	userContactsLabel.setStyleName("footerTitleLabel");

	RegExpValidator emailValidator = new RegExpValidator();
	emailValidator.setErrorMessage("Invalid email address");
	emailValidator.setExpression("^([a-zA-Z0-9_.\\-+])+@(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,4}$");

	ButtonItem addNewAvailableContactButton = new ButtonItem("ok");
	addNewAvailableContactButton.setEndRow(false);

	final DynamicForm newAvailableContactDynamicForm = new DynamicForm();
	newAvailableContactDynamicForm.setVisible(false);
	newAvailableContactDynamicForm.setAutoHeight();
	newAvailableContactDynamicForm.setItemLayout(FormLayoutType.TABLE);
	newAvailableContactDynamicForm.setBackgroundColor("#FFFFFF");
	newAvailableContactDynamicForm.setBorder("1px solid");
	newAvailableContactDynamicForm.setPadding(5);
	newAvailableContactDynamicForm.setMargin(5);

	PickerIcon okPicker = new PickerIcon(PickerIcon.CLEAR, new FormItemClickHandler() {
	    public void onFormItemClick(FormItemIconClickEvent event) {
		if (newAvailableContactDynamicForm.validate()) {
		    PreparedRequest prepReq = new PreparedRequest("user", "contact/add", "contactMail",
			    newAvailableContactDynamicForm.getValueAsString("email"), "parentUserId", LogIn.getUserDto().getUserId()
				    .toString());
		    prepReq.sendAsyncRequest(new NewAvailableContactRequestCallback());
		    FormItem fi = newAvailableContactDynamicForm.getField("email");
		    fi.clearValue();
		}
	    }
	});

	final TextItem emailTextItem = new TextItem();
	emailTextItem.setName("email");
	emailTextItem.setTitle("E-Mail");
	emailTextItem.setRequired(true);
	emailTextItem.setValidators(emailValidator);
	emailTextItem.setEndRow(false);
	emailTextItem.setWrapTitle(false);
	emailTextItem.setIcons(okPicker);

	newAvailableContactDynamicForm.setItems(emailTextItem);
	newAvailableContactDynamicForm.setPosition(Positioning.RELATIVE);
	newAvailableContactDynamicForm.setLeft("0%");

	Button newAvailableContactButton = new Button("+");
	newAvailableContactButton.setWidth(15);
	newAvailableContactButton.setHeight(14);
	newAvailableContactButton.setStyleName("topMargin");
	newAvailableContactButton.addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {
		if (!newAvailableContactDynamicForm.isVisible()) {
		    newAvailableContactDynamicForm.animateShow(AnimationEffect.FADE, null, 500);
		} else {
		    newAvailableContactDynamicForm.animateHide(AnimationEffect.FADE, null, 200);
		    FormItem fi = newAvailableContactDynamicForm.getField("email");
		    fi.clearValue();
		}
	    }
	});

	HStack headerUserContactsStack = new HStack();
	headerUserContactsStack.setWidth("*");
	headerUserContactsStack.setHeight(titleHeight);
	headerUserContactsStack.addMember(userContactsLabel);
	headerUserContactsStack.addMember(newAvailableContactButton);

	addChild(newAvailableContactDynamicForm);
	addMember(headerUserContactsStack);
    }

}
